//选择默认企业
imports('../../../style/default')

var layer = require('layer');
var template = require('template');
var checkradio = require('{plus}/checkradio');
var func = require('{pub}/function');

var tpl = renders({
    <form class="ui-form m-default">
        <div class="f-oa scroll">
            <div class="inner">
            <%var isDefault%>
            <%each $list%>
            <div class="ui-item e-mb30">
                <span class="ui-checkradio">
                    <input type="radio" name="companyId" 
                    <%if !isDefault && (isDefault = !!$value.isDefault)%>
                    checked="checked"
                    <%/if%>
                    data-id="<%$value.id%>" value="<%$value.id%>">
                    <b class="e-pl10"><%$value.name%></b>
                </span>
                <%if $value.useStatus == '2'%>
                <%if $value.useType == '1'%>
                <p>该企业试用到期 <a class="f-cblue j-buy">立即购买</a></p>
                <%elseif $value.useType == '2'%>
                <p>该企业帐号到期 <a class="f-cblue j-buy">立即续费</a></p>
                <%/if%>
                <%/if%>
            </div>
            <%/each%>
            </div>
        </div>
        <div class="ui-item e-pt10 e-pl15">
            <button type="button" class="ui-button ui-button-normal<%if !isDefault%> ui-button-disabled<%/if%> j-submit">确定</button>
        </div>
        <div class="ui-item e-mt15">
            <span class="ui-checkradio">
                <input type="checkbox" name="setDefault" value="1">
                <b class="e-pl10">设置为默认登录企业，下次自动登录</b>
            </span>
        </div>
    </form>
})

return function(){
    var that = this;
    var _layer = layer({
        template:tpl,
        data:that.companyList,
        title:'选择登录企业',
        width:420,
        cancel:{
            enable:false
        },
        close:{
            enable:!!that.data.company.id
        },
        events:{
            'click .j-submit':'enabled submit'
        },
        onInit:function(self){
            self.element.find(':checkbox, :radio').checkradio({
                callback:function(ele){
                    if(ele.is(':radio')){
                        self.element.find('.ui-button-disabled').removeClass('ui-button-disabled')
                    }
                }
            })
        },
        enabled:func.enabled,
        submit:function(e, elem){
            var data = require('util').getData(_layer.element.find('form')).result;
            require('request').update('company/enter', data, function(){
                that.renderCompany(_layer.element.find(':radio:checked').data('id'));
                _layer.destroy();
            }, _layer)
        }
    })
}